package com.java2404.micro.order.handler;


import com.java2404.micro.common.dto.GoodsDTO;
import com.java2404.micro.common.dto.OrderDTO;
import com.java2404.micro.common.entity.Order;
import com.java2404.micro.common.entity.PageBean;
import com.java2404.micro.common.resp.Result;
import com.java2404.micro.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @User userlq
 * @Date 2025/1/5 19:07
 * @Classname Ordercontroller
 * @Description 订单处理器
 */
@Slf4j
@Api(tags = "订单模块")
@RestController
@RequestMapping("order")
public class OrderHandler {

    @Resource
    private OrderService orderService;

    /**
     * 测试控制器，仅仅用来测试
     *
     * @return String
     */
    @ApiOperation(value = "测试")
    @GetMapping("test")
    public String test() {
        return "test handler...";
    }

    /**
     * 添加订单
     *
     * @param order 订单对象
     * @return Result结果集
     */
    @ApiOperation(value = "添加订单")
    @PostMapping("addOrder")
    public Result addOrder(@RequestBody Order order) {
        boolean save = orderService.save(order);
        return save ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 分页查询所有订单信息
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @return Result结果集
     */
    @ApiOperation(value = "分页查询所有订单")
    @GetMapping("showAllOrders")
    public Result<PageBean<OrderDTO>> showAllOrders(@RequestParam(defaultValue = "1") Integer pageNum,
                                                    @RequestParam(defaultValue = "2") Integer pageSize) {
        PageBean<OrderDTO> orderDTOPageBean = orderService.showAllOrders(pageNum, pageSize);
        return orderDTOPageBean.getTotal() == 0 ? Result.error("查询失败") : Result.success(orderDTOPageBean);
    }

    /**
     * 分页查询商家所有订单信息
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @param mid      商家id
     * @return Result结果集
     */
    @ApiOperation(value = "分页查询商家所有订单")
    @GetMapping("showAllOrdersBymid")
    public Result<PageBean<OrderDTO>> showAllOrdersBymid(@RequestParam(defaultValue = "1") Integer pageNum,
                                                         @RequestParam(defaultValue = "2") Integer pageSize,
                                                         @RequestParam Integer mid) {
        PageBean<OrderDTO> orderDTOPageBean = orderService.showAllOrdersBymid(pageNum, pageSize, mid);
        return orderDTOPageBean.getTotal() == 0 ? Result.error("查询失败") : Result.success(orderDTOPageBean);
    }

    /**
     * 分页查询用户所有订单信息
     *
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @param uid      用户id
     * @return Result结果集
     */
    @ApiOperation(value = "分页查询用户所有订单")
    @GetMapping("showAllOrdersByuid")
    public Result<PageBean<OrderDTO>> showAllOrdersByuid(@RequestParam(defaultValue = "1") Integer pageNum,
                                                         @RequestParam(defaultValue = "2") Integer pageSize,
                                                         @RequestParam Integer uid) {
        PageBean<OrderDTO> orderDTOPageBean = orderService.showAllOrdersByuid(pageNum, pageSize, uid);
        return orderDTOPageBean.getTotal() == 0 ? Result.error("查询失败") : Result.success(orderDTOPageBean);
    }

    /**
     * 测试goodsHandler 查询商品列表
     *
     * @return
     */
    @GetMapping("show")
    public Result<PageBean<GoodsDTO>> show() {
        return orderService.ShowGoods(1, 2);
    }


}
